((PLUGIN_ID) => {
    const config = kintone.plugin.app.getConfig(PLUGIN_ID);
    if (!('api_key' in config) || !('api_secret' in config)) {
        return;
    }
    const API_KEY = config.api_key;
    const API_SECRET = config.api_secret;

    let _checkIsLoadScript = src => {
        let scriptObjs = document.getElementsByTagName('script');
        for (let sObj of scriptObjs) {
            if (sObj.src == src) {
                return true;
            }
        }
        return false;
    }

    let loadScript = event => {
        let src = "https://source.zoom.us/zoom-meeting-1.7.10.min.js";
        return new Promise(resolve => {
            if (_checkIsLoadScript(src)) {
                resolve(event);
            }
            let scriptNode = document.createElement("script");
            scriptNode.setAttribute("type", "text/javascript");
            scriptNode.setAttribute("src", src);
            document.body.appendChild(scriptNode);
            if (scriptNode.readyState) {
                scriptNode.onreadystatechange = () => {
                    if (scriptNode.readyState == "complete" || scriptNode.readyState == 'loaded') {
                        resolve(event);
                    }
                }
            } else {
                scriptNode.onload = () => {
                    console.log("script loaded");
                    resolve(event);
                }
            }
        })
    }

    let zoomStart = event => {
        let record = event.record;
        let meetingConfig = {
            apiKey: API_KEY,
            meetingNumber: record.meetingNumber.value,
            userName: (() => {
                if (record.name.value) {
                    try {
                        return testTool.b64DecodeUnicode(record.name.value);
                    } catch (e) {
                        return record.name.value;
                    }
                }
                return "Guest";
            })(),
            passWord: record.password.value,
            leaveUrl: "/k/#/portal",
            role: parseInt(record.role.value, 10),
            lang: record.lang.value,
            // china: record.china.value === "1",
        };

        if (testTool.isMobileDevice()) {
            vConsole = new VConsole();
        }

        if (meetingConfig.china)
            ZoomMtg.setZoomJSLib("https://jssdk.zoomus.cn/1.7.10/lib", "/av");

        ZoomMtg.preLoadWasm();
        ZoomMtg.prepareJssdk();

        let beginJoin = signature => {
            ZoomMtg.init({
                leaveUrl: meetingConfig.leaveUrl,
                success: () => {
                    $.i18n.reload(meetingConfig.lang);
                    ZoomMtg.join({
                        meetingNumber: meetingConfig.meetingNumber,
                        userName: meetingConfig.userName,
                        signature: signature,
                        apiKey: meetingConfig.apiKey,
                        passWord: meetingConfig.passWord,
                        success: res => {
                            ZoomMtg.getAttendeeslist({});
                            ZoomMtg.getCurrentUser({
                                success: res => {
                                },
                            });
                        },
                        error: res => {
                            console.log(res);
                        },
                    });
                },
                error: res => {
                    console.log(res);
                },
            });
        }

        ZoomMtg.generateSignature({
            meetingNumber: meetingConfig.meetingNumber,
            apiKey: API_KEY,
            apiSecret: API_SECRET,
            role: meetingConfig.role,
            success: res => {
                meetingConfig.signature = res.result;
                meetingConfig.apiKey = API_KEY;
                beginJoin(meetingConfig.signature);
            },
        });
    }

    let handler = event => {
        loadScript(event).then(zoomStart);
    };

    kintone.events.on('app.record.detail.show', handler);
})(kintone.$PLUGIN_ID);